package com.msconfig.widget;

/*
 * author: caojunhao
 * date  : 2020/5/14
 * desc  :绘制星图上面的小卫星
 */

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;

import com.geo.msconfig.R;
import com.geo.parse.GnssSateInfo;
import com.geo.parse.GnssSateInfoList;
import com.msconfig.utils.DisplayUtils;

public class DrawSatellite extends View {

    private int mEarthCircleX;
    private int mEarthCircleY;
    private int mSatelliteRadius;
    private int mTextSize;
    private int mEarthRadius;
    private Context mContext;

    private Paint mPaint;
    private int mGpsColor;
    private int mGlonassColor;
    private int mBeidouColor;
    private int mGalileoColor;
    private int mNusedColor;
    private int mColor= mNusedColor;
    private GnssSateInfoList mGnssSateInfoList;
    private GnssSateInfo gnssSateInfo;


    public DrawSatellite(Context context, int width, int heigth, GnssSateInfoList mGnssSateInfoList) {
        this(context,null);
        this.mEarthCircleX = width/2;//540
        this.mEarthCircleY = heigth/2;//
        this.mEarthRadius= mEarthCircleX-mEarthCircleX/4;//
        this.mSatelliteRadius = mEarthRadius/10;
        this.mTextSize = mEarthRadius/12;
        this.mGnssSateInfoList=mGnssSateInfoList;

    }

    public DrawSatellite(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public DrawSatellite(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext=context;
        init(context,attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        mGpsColor = ContextCompat.getColor(context,R.color.gpscolor);
        mGlonassColor = ContextCompat.getColor(context, R.color.glonasscolor);
        mBeidouColor = ContextCompat.getColor(context,R.color.beidoucolor);
        mGalileoColor = ContextCompat.getColor(context,R.color.galileocolor);
        mNusedColor = ContextCompat.getColor(context,R.color.nusedcolor);

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Style.STROKE);
        mPaint.setColor(Color.BLUE);
        mPaint.setTextSize(DisplayUtils.px2sp(mContext,80));
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawCircle(canvas);
    }

    private void drawCircle(Canvas canvas){
        if(mGnssSateInfoList==null)
            return;
        for(int i=0;i<mGnssSateInfoList.getSateInfoList().size();i++){
            gnssSateInfo=mGnssSateInfoList.getSateInfoList().get(i);
            switch (gnssSateInfo.getType().swigValue()){
                case 1://SATSYS_GPS
                    mColor= mGpsColor;
                    break;
                case 2://SATSYS_GLONASS
                    mColor= mGlonassColor;
                    break;
                case 3://SATSYS_SBAS
                    break;
                case 4://SATSYS_GALILEO
                    mColor= mGalileoColor;
                    break;
                case 5://SATSYS_BD
                    mColor= mBeidouColor;
                    break;
                case 6://SATSYS_QZSS
                    break;
            }

            if(gnssSateInfo.getStatus().swigValue()!=2){
                mColor= mNusedColor;
            }
            drawSatellite(canvas,gnssSateInfo,mColor);
        }

    }

    /**
     * 在背景上绘制卫星
     * @param canvas
     * @param gnssSateInfo
     */
    private void drawSatellite(Canvas canvas, GnssSateInfo gnssSateInfo, int color) {
        if(gnssSateInfo==null)
            return;
        mPaint.setColor(color);
        mPaint.setStyle(Style.FILL_AND_STROKE);
        /**
         * GPS卫星导航仪通常选用仰角大于5º，小于85º。 因为当卫星仰角大于85º时，L1波段的电离层折射误差较大，故规定仰角大于85º时，
         * 定位无效，不进行数据更新。而卫星仰角越小，则对流层折射误差越大，故一般选用仰角大于5º的卫星来定位。
         */
        //得到仰角
        float elevation = gnssSateInfo.getElevation();
        double elevationRadian = ((elevation) * Math.PI) / 180.0d;
        //通过仰角，计算出这个卫星应该绘制到离圆心多远的位置
        double r2 = Math.cos(elevationRadian)* mEarthRadius ;

        /*得到方位角（与正北向也就是Y轴顺时针方向的夹角，注意我们通常几何上的角度
         * 是与X轴正向的逆时针方向的夹角）,在计算X，Y座标的三角函数时，要做转换
         */
        double azimuth=gnssSateInfo.getAzimuth();
        double radian = (azimuth * Math.PI) / 180.0d;
        double x = this.mEarthCircleX + Math.sin(radian) * r2;
        double y = this.mEarthCircleY - Math.cos(radian) * r2;

        //以x,y为中心绘制卫星圆
        canvas.drawCircle((int)x,(int)y, mSatelliteRadius, mPaint);
        //在卫星图标的位置上绘出文字（卫星编号及信号强度）
        mPaint.setTextSize(mTextSize);
        mPaint.setStyle(Style.FILL_AND_STROKE);
        mPaint.setColor(Color.BLACK);
        mPaint.setTextAlign(Paint.Align.LEFT);
        String info= String.valueOf(gnssSateInfo.getPrn());

        Rect rect=getTextMeasure(info, mPaint);
        canvas.drawText(info, (float) (x-(rect.width()/2)), (float) (y+(rect.height()/2)), mPaint);
    }

    public Rect getTextMeasure(String str, Paint p){
        //1. 返回包围串的最小的一个rect区域
        Rect rect = new Rect();
        p.getTextBounds(str,0,str.length(),rect);
        return rect;
    }

}

